﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DyData
{
    public partial class OutputForm : Form
    {


        public RichTextBox txtOutput = null!;

        private void InitializeComponent()
        {
            txtOutput = new RichTextBox();
            SuspendLayout();
            // 
            // txtOutput
            // 
            txtOutput.Dock = DockStyle.Fill;
            txtOutput.Location = new Point(0, 0);
            txtOutput.Name = "txtOutput";
            txtOutput.ReadOnly = true;
            txtOutput.Size = new Size(1069, 338);
            txtOutput.TabIndex = 0;
            txtOutput.Text = "";
            // 
            // OutputForm
            // 
            ClientSize = new Size(1069, 338);
            Controls.Add(txtOutput);
            Name = "OutputForm";
            ResumeLayout(false);
        }

        public OutputForm()
        {
            //txtOutput = new RichTextBox();
            InitializeComponent();
            this.Shown += (s, e) => { this.txtOutput.ScrollToCaret(); };
        }

        // 添加文本到输出区域（线程安全）
        public void AppendText(string text, Color? color = null)
        {
            if (this.txtOutput.InvokeRequired)
            {
                this.Invoke((Action<string, Color?>)AppendText, text, color);
                return;
            }

            Color textColor = color ?? this.txtOutput.ForeColor;
            this.txtOutput.SelectionStart = this.txtOutput.TextLength;
            this.txtOutput.SelectionColor = textColor;
            this.txtOutput.AppendText($"{DateTime.Now:HH:mm:ss.fff} - {text}{Environment.NewLine}");
            this.txtOutput.ScrollToCaret();
        }

        // 清空输出
        public void ClearOutput()
        {
            if (this.txtOutput.InvokeRequired)
            {
                this.Invoke((Action)ClearOutput);
                return;
            }

            this.txtOutput.Clear();
        }



        private void button1_Click(object sender, EventArgs e)
        {

        }

    }

    public enum LogLevel
    {
        Info,
        Debug,
        Warning,
        Error
    }

    public class Logger
    {
        private RichTextBox _logTextBox;
        private LogLevel _minLogLevel = LogLevel.Info;

        public Logger(RichTextBox logTextBox)
        {
            _logTextBox = logTextBox;
        }

        public void SetMinLogLevel(LogLevel level)
        {
            _minLogLevel = level; //小于此值不显示
        }

        private Color GetLogLevelColor(LogLevel level)
        {
            return level switch
            {
                LogLevel.Info => Color.Black,
                LogLevel.Debug => Color.Gray,
                LogLevel.Warning => Color.DarkOrange,
                LogLevel.Error => Color.Red,
                _ => Color.Black
            };
        }

        public void Log(LogLevel level, string message)
        {
            if (level < _minLogLevel) return;

            // 确保在UI线程更新
            if (_logTextBox.InvokeRequired)
            {
                _logTextBox.Invoke(new Action(() => Log(level, message)));
                return;
            }

            // 根据日志级别设置颜色
            Color color = GetLogLevelColor(level);

            // 添加带时间戳的日志
            string levelName = Enum.GetName(typeof(LogLevel), level);
            string logEntry = $"[{DateTime.Now:HH:mm:ss}] {levelName.ToUpper()}: {message}{Environment.NewLine}";

            // 保存当前选择位置
            int selectionStart = _logTextBox.SelectionStart;
            int selectionLength = _logTextBox.SelectionLength;

            // 追加日志
            _logTextBox.AppendText(logEntry);

            // 设置新文本的颜色
            int startIndex = _logTextBox.Text.Length - logEntry.Length+1;
            _logTextBox.Select(startIndex, logEntry.Length);
            _logTextBox.SelectionColor = color;

            // 恢复之前的选择
            _logTextBox.Select(selectionStart, selectionLength);

            // 滚动到最后
            _logTextBox.SelectionStart = _logTextBox.Text.Length;
            _logTextBox.ScrollToCaret();
        }

        public void LogInfo(string message) => Log(LogLevel.Info, message);
        public void LogDebug(string message) => Log(LogLevel.Debug, message);
        public void LogWarning(string message) => Log(LogLevel.Warning, message);
        public void LogError(string message) => Log(LogLevel.Error, message);

    } 


}
